#ifndef _BATTERY_H_
#define _BATTERT_H_

#include "stm8s_gpio.h"
#include "stm8s_adc1.h"
#include "config.h"
#include "led.h"

#define VOLBUF_LENGTH 32
#define ON_OUTPUT GPIO_WriteHigh(g_pBattery->powerOutputPort, g_pBattery->powerOutputPin)
#define OFF_OUTPUT GPIO_WriteLow(g_pBattery->powerOutputPort, g_pBattery->powerOutputPin)
#define ON_SHORT GPIO_WriteHigh(g_pBattery->shortPort, g_pBattery->shortPin)

typedef void (* f_getVolAvg)(void);
void saveDelVol(short newData, short *dataBuf);


typedef struct battery_s
{
    GPIO_TypeDef* voltagePort;
    GPIO_Pin_TypeDef voltagePin;
    GPIO_TypeDef* powerChipPort;
    GPIO_Pin_TypeDef powerChipPin;
    GPIO_TypeDef* powerOutputPort;
    GPIO_Pin_TypeDef powerOutputPin;
    GPIO_TypeDef* switchKeyPort;
    GPIO_Pin_TypeDef switchKeyPin;
    GPIO_TypeDef* shortPort;//短路保护
    GPIO_Pin_TypeDef shortPin;
    u16 volDataBuf[VOLBUF_LENGTH];
    u8 volIndex;
    f_getVolAvg calculateVoltage;
    double voltage;
    double realVol;
    u8 status;
}battery_t;



extern const battery_t * const g_pBattery;
void batteryInit(void);
void getVolAvgData(void);
void judgeStatus(void);
void calculateRealVol(void);
u8 stableJudge(void);
void temProtect(void);
void volToLedStatus(void);
#endif
